package com.songlanyun.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ImgUtil {

    /**
     * 将富文本的图片的 src 获取出来，用逗号拼接
     *
     * @param htmlStr
     * @return
     */
    public static String getImgStr(String htmlStr) {
        StringBuilder imgStr = new StringBuilder();
        String img = "";
        Pattern p_image;
        Matcher m_image;
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";//图片链接地址
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                imgStr.append(m.group(1)).append(",");
            }
        }
        return imgStr.toString();
    }
}
